#include "gtest/gtest.h"
#include "linkedlist.hpp"

using namespace yfwz100::fundamental;

class LinkedListTest : public ::testing::Test
{
protected:
  virtual void SetUp()
  {
    for (int i=0; i<10; i++) {
      list.push_back(i);
    }
  }

  LinkedList<int> list;
  LinkedList<int> emptyList;
};

TEST_F(LinkedListTest, deletion)
{
  list.clear();
  ASSERT_TRUE(list.isEmpty());
  ASSERT_EQ(list.size(), 0);
}

TEST_F(LinkedListTest, remove_by_index)
{
  list.remove_by_index(1);
  ASSERT_EQ(list[1], 2);
}

TEST_F(LinkedListTest, removeByValue)
{
  list.remove_by_value(1);
  ASSERT_EQ(list[1], 2);
}

TEST_F(LinkedListTest, modification)
{
  list[1] = 2;
  ASSERT_EQ(list[1], 2);
}

TEST_F(LinkedListTest, iterate)
{
  // iterate by prefix addition.
  int i = 0;
  for (auto itr = list.begin(); itr != list.end(); ++itr) {
    ASSERT_EQ(*itr, i++);
  }
  // iterate by postfix addition.
  i = 0;
  for (auto itr = list.begin(); itr != list.end();) {
    ASSERT_EQ(*(itr++), i++);
  }
}

TEST_F(LinkedListTest, random_access)
{
  ASSERT_EQ(list[1], 1);
  ASSERT_EQ(list[9], 9);
  ASSERT_EQ(*(list.begin() + 1), 1);
  ASSERT_EQ(*(list.begin() + 9), 9);
}

TEST_F(LinkedListTest, empty)
{
  ASSERT_TRUE(emptyList.isEmpty());
  ASSERT_EQ(emptyList.size(), 0);
}

TEST_F(LinkedListTest, size)
{
  ASSERT_EQ(list.size(), 10);
}
